package de.szut.traineeslife.utils;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.TimeZone;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import android.util.Log;

public class DateHelper implements Serializable {

	private static Calendar calendar = Calendar.getInstance(TimeZone.getDefault());

	private static ArrayList<Integer> numberWeeksOfMonth = new ArrayList<Integer>();
	private static ArrayList<Integer> numberWeeksOfYear = new ArrayList<Integer>();

	private static ArrayList<Integer> numberDaysOfMonth = new ArrayList<Integer>();

	private ArrayList<String> weeksOfMonth = new ArrayList<String>();
	private ArrayList<String> weeksOfYear = new ArrayList<String>();
	private ArrayList<String> startDates = new ArrayList<String>();
	private ArrayList<String> endDates = new ArrayList<String>();
	private DateTime educationFrom;
	private DateTime educationTo;
	private DateTime selectedYear;
	private ArrayList<Integer> monthOfSelectedYear = new ArrayList<Integer>();
	private int currentYear = calendar.get(Calendar.YEAR);
	private int currentMonth = calendar.get(Calendar.MONTH);
	private int currentDay = calendar.get(Calendar.DAY_OF_MONTH);

	public DateHelper() {
	}

	/**gets the Number of Days from specific month 
	 * @param month
	 * @param year
	 * @return
	 */
	public static ArrayList<Integer> getNumberDaysOfMonth(int month, int year) {
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, 1);

		int ndays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		int days[] = new int[ndays];
		for (int i = 0; i < ndays; i++) {
			numberDaysOfMonth.add(calendar.get(Calendar.WEEK_OF_YEAR));
			calendar.add(Calendar.DATE, 1);
		}
		return numberDaysOfMonth;
	}

	/**calculates the number of weeks based on the days of the Month
	 * @param daysOfMonth
	 * @return
	 */
	public static ArrayList<Integer> getWeeksOfYear(ArrayList<Integer> daysOfMonth) {
		for (int j : daysOfMonth) {
			int week = j;
			if (numberWeeksOfYear.size() > 0) {
				for (int b = 0; b < numberWeeksOfYear.size(); b++) {
					if (numberWeeksOfYear.contains(week) == false) {
						numberWeeksOfYear.add(week);
					}
				}
			} else {
				numberWeeksOfYear.add(week);
			}
		}
		return numberWeeksOfYear;
	}
	
	/** Calculates the Start and the en of a Week
	 * @param enterWeek
	 * @param enterYear
	 */
	public void getStartEndOFWeek(int enterWeek, int enterYear) {
		calendar.clear();
		calendar.set(Calendar.WEEK_OF_YEAR, enterWeek);
		calendar.set(Calendar.YEAR, enterYear);

		SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yy");
		Date startDate = calendar.getTime();
		String startDateInStr = formatter.format(startDate);

		calendar.add(Calendar.DATE, 4);
		Date enddate = calendar.getTime();
		String endDateString = formatter.format(enddate);
		startDates.add(startDateInStr);
		endDates.add(endDateString);
		weeksOfYear.add(startDateInStr + " - " + endDateString);
	}

	/**Calculates the Weeks of the Year that the User has selected
	 * @return
	 */
	public String[] getWeeksOfSelectedYear() {
		int year = getSelectedYear().getYear();
		for (int i = 0; i < 12; i++) {
			getWeeksOfYear(getNumberDaysOfMonth(i, year));
		}
		for (int i=0;i< numberWeeksOfYear.size();i++) {
			getStartEndOFWeek(i, year);
		}
		String[] weeksOfYearString = new String[weeksOfYear.size()];
		for (int j = 0; j < weeksOfYear.size(); j++) {
			weeksOfYearString[j] = weeksOfYear.get(j);
		}
		weeksOfYear.clear();
		startDates.clear();
		endDates.clear();
		return weeksOfYearString;

	}

	/**Calculates the Years of Education by users data
	 * @return
	 */
	public String[] getYearsOfEducation() {
		int years = getEducationTo().getYear() - getEducationFrom().getYear();
		DateTime amountYearDate;
		ArrayList<DateTime> amountYearList = new ArrayList<DateTime>();
		for (int j = 1; j < years; j++) {
			amountYearDate = getEducationFrom().plusYears(j);
			amountYearList.add(amountYearDate);
		}
		ArrayList<String> listOfYeras = new ArrayList<String>();
		listOfYeras.add(Integer.toString(getEducationFrom().getYear()));
		for (int k = 0; k < amountYearList.size(); k++) {
			listOfYeras.add(Integer.toString(amountYearList.get(k).getYear()));
		}
		listOfYeras.add(Integer.toString(getEducationTo().getYear()));
		String[] yearsOfEduccation = new String[listOfYeras.size()];
		for (int i = 0; i < listOfYeras.size(); i++) {
			yearsOfEduccation[i] = listOfYeras.get(i);
		}
		return yearsOfEduccation;

	}

	public ArrayList<String> getWeeksOfMonth() {
		return weeksOfMonth;
	}

	public void setWeeksOfMonth(ArrayList<String> weeksOfMonth) {
		this.weeksOfMonth = weeksOfMonth;
	}

	public int getCurrentYear() {
		return currentYear;
	}

	public void setCurrentYear(int currentYear) {
		this.currentYear = currentYear;
	}

	public int getCurrentMonth() {
		return currentMonth;
	}

	public void setCurrentMonth(int currentMonth) {
		this.currentMonth = currentMonth;
	}

	public int getCurrentDay() {
		return currentDay;
	}

	public void setCurrentDay(int currentDay) {
		this.currentDay = currentDay;
	}

	public DateTime getEducationFrom() {
		return educationFrom;
	}

	public void setEducationFrom(Date fromDate) {
		DateTime dt = new DateTime(fromDate);
		this.educationFrom = dt;
	}

	public DateTime getEducationTo() {
		return educationTo;
	}

	public void setEducationTo(Date toDate) {
		DateTime dt = new DateTime(toDate);
		this.educationTo = dt;
	}

	public DateTime getSelectedYear() {
		return selectedYear;
	}

	
	public ArrayList<String> getStartDates() {
		return startDates;
	}

	public void setStartDates(ArrayList<String> startDates) {
		this.startDates = startDates;
	}

	public ArrayList<String> getEndDates() {
		return endDates;
	}

	public void setEndDates(ArrayList<String> endDates) {
		this.endDates = endDates;
	}

	public void setSelectedYear(String selectedYear) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
		DateTime dt = new DateTime(formatter.parseDateTime("01/01/" + selectedYear + " 00:00:00"));
		this.selectedYear = dt;
	}

}